package com.huo.thread.exchangerdemo;

import java.util.concurrent.Exchanger;

public class ExchangerDemo {
    public void testMethod(Exchanger<String> exchanger){
        System.out.println(Thread.currentThread().getName()+"执行中。。。");
        String value = "";
        try {
            Thread.sleep(1000);
            value = exchanger.exchange(Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() +"交互的值" + value);
    }

    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();
        ExchangerDemo demo = new ExchangerDemo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.testMethod(exchanger);
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.testMethod(exchanger);
            }
        }).start();

    }
}
